﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class MineralizationForm : Form
    {
        tb_Mineralization mineralization;

        int regID, avpID, year, month;


        public MineralizationForm()
        {
            InitializeComponent();
        }

        private void MineralizationForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            string[] month;
            if (InformationClass.Lang)
                month = new string[] { "1 Апреля", "1 Июля", "1 Октября" };
            else
                month = new string[] { "1 Апрел", "1 Июл", "1 Октябр" };

            for (int i = 1; i <= month.Length; i++)
            {
                var m = new
                {
                    ID = i,
                    Name = month[i - 1]
                };
                cbMonth.Items.Add(m);
            }
            cbMonth.DisplayMember = "Name";
            cbMonth.ValueMember = "ID";

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }

            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());
            this.year = Convert.ToInt32(cbYear.Text);

        }

        private void ShowMineralization(int avpID, int period, int year)
        {
            if (avpID > 0 && period > 0 && year > 0)
            {
                DateTime dt;

                switch (period)
                {
                    case 1:
                        dt = new DateTime(year, 4, 1);
                        break;
                    case 2:
                        dt = new DateTime(year, 7, 1);
                        break;
                    case 3:
                        dt = new DateTime(year, 10, 1);
                        break;
                    default:
                        dt = new DateTime();
                        break;
                }

                mineralization = DBClass.Mineralization.Where(wh => wh.AVPID == avpID && wh.Date == dt).SingleOrDefault();
                if (mineralization != null)
                {
                    this.tbAreaObserved.Text = mineralization.AreaObserved.ToString();
                    this.tbGW01.Text = mineralization.GW01.ToString();
                    this.tbGW12.Text = mineralization.GW12.ToString();
                    this.tbGW23.Text = mineralization.GW23.ToString();
                    this.tbGW35.Text = mineralization.GW35.ToString();
                    this.tbGW5.Text = mineralization.GW5.ToString();
                    this.tbSGW01.Text = mineralization.SGW01.ToString();
                    this.tbSGW13.Text = mineralization.SGW13.ToString();
                    this.tbSGW35.Text = mineralization.SGW35.ToString();
                    this.tbSGW510.Text = mineralization.SGW510.ToString();
                    this.tbSGW10.Text = mineralization.SGW10.ToString();
                    this.tblow.Text = mineralization.low.ToString();
                    this.tbaverage.Text = mineralization.average.ToString();
                    this.tbhigh.Text = mineralization.high.ToString();
                }
                else
                {
                    tbAreaObserved.Text = tbaverage.Text = tbGW01.Text = tbGW12.Text = tbGW23.Text = tbGW35.Text =
                        tbGW5.Text = tbhigh.Text = tblow.Text = tbSGW01.Text = tbSGW10.Text = tbSGW13.Text =
                        tbSGW35.Text = tbSGW510.Text = string.Empty;
                }
            }
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex == -1)
                return;

            regID = Convert.ToInt32(cbRegion.SelectedValue);
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.ValueMember = "ID";
            cbAVP.DisplayMember = "Name";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            cbAVP.Enabled = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (avpID > 0 && month > 0 && year > 0)
            {
                DateTime dt;

                switch (month)
                {
                    case 1:
                        dt = new DateTime(year, 4, 1);
                        break;
                    case 2:
                        dt = new DateTime(year, 7, 1);
                        break;
                    case 3:
                        dt = new DateTime(year, 10, 1);
                        break;
                    default:
                        dt = new DateTime();
                        break;
                }

                decimal arObs, gw01, gw12, gw23, gw35, gw5, sgw01, sgw13, sgw35, sgw510, sgw10, low, average, high;

                bool correct = tbAreaObserved.Text.GetDecimalOrDefault(out arObs);
                correct &= tbaverage.Text.GetDecimalOrDefault(out average);
                correct &= tbGW01.Text.GetDecimalOrDefault(out gw01);
                correct &= tbGW12.Text.GetDecimalOrDefault(out gw12);
                correct &= tbGW23.Text.GetDecimalOrDefault(out gw23);
                correct &= tbGW35.Text.GetDecimalOrDefault(out gw35);
                correct &= tbGW5.Text.GetDecimalOrDefault(out gw5);
                correct &= tbhigh.Text.GetDecimalOrDefault(out high);
                correct &= tblow.Text.GetDecimalOrDefault(out low);
                correct &= tbSGW01.Text.GetDecimalOrDefault(out sgw01);
                correct &= tbSGW10.Text.GetDecimalOrDefault(out sgw10);
                correct &= tbSGW13.Text.GetDecimalOrDefault(out sgw13);
                correct &= tbSGW35.Text.GetDecimalOrDefault(out sgw35);
                correct &= tbSGW510.Text.GetDecimalOrDefault(out sgw510);

                if (!correct)
                {
                    if (MessageBox.Show(msgClass.msgUncorrectData_Continue, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                }

                if (mineralization != null)
                {
                    mineralization.AreaObserved = arObs;
                    mineralization.average = average;
                    mineralization.GW01 = gw01;
                    mineralization.GW12 = gw12;
                    mineralization.GW23 = gw23;
                    mineralization.GW35 = gw35;
                    mineralization.GW5 = gw5;
                    mineralization.high = high;
                    mineralization.low = low;
                    mineralization.SGW01 = sgw01;
                    mineralization.SGW10 = sgw10;
                    mineralization.SGW13 = sgw13;
                    mineralization.SGW35 = sgw35;
                    mineralization.SGW510 = sgw510;
                }
                else 
                {
                    mineralization = new tb_Mineralization
                    {
                        AreaObserved = arObs,
                        average = average,
                        AVPID = avpID,
                        Date = dt,
                        GW01 = gw01,
                        GW12 = gw12,
                        GW23 = gw23,
                        GW35 = gw35,
                        GW5 = gw5,
                        high = high,
                        low = low,
                        SGW01 = sgw01,
                        SGW10 = sgw10,
                        SGW13 = sgw13,
                        SGW35 = sgw35,
                        SGW510 = sgw510
                    };
                    DBClass.Mineralization.InsertOnSubmit(mineralization);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }

            }
        }

        private void ClearForm(bool state)
        {
            groupBox1.Enabled = groupBox2.Enabled = groupBox3.Enabled = tbAreaObserved.Enabled = state;
            tbAreaObserved.Text = tbGW01.Text = tbGW12.Text = tbGW23.Text = tbGW35.Text = tbGW5.Text =
            tbSGW01.Text = tbSGW13.Text = tbSGW35.Text = tbSGW510.Text = tbSGW10.Text = tblow.Text =
            tbaverage.Text = tbhigh.Text = string.Empty;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);

                ShowMineralization(avpID, month, year);
            }
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Text);
                ShowMineralization(avpID, month, year);
            }
            else
                year = 0;
        }

        private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbMonth.SelectedIndex >= 0)
            {
                month = cbMonth.SelectedIndex + 1;
                ShowMineralization(avpID, month, year);
            }
            else
                month = 0;
        }




    }
}
